# Batch convert all .ppt/.pptx files encountered in folder and all its subfolders
# The produced PDF files are stored in the invocation folder
#
# Adapted from http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf
# Thanks to MFT, takabanana, ComFreek
#
# If PowerShell exits with an error, check if unsigned scripts are allowed in your system.
# You can allow them by calling PowerShell as an Administrator and typing
# ```
# Set-ExecutionPolicy Unrestricted
# ```
# Get invocation path
$curr_path = Get-Location
Write-Output $curr_path
# Create a PowerPoint object
$ppt_app = New-Object -ComObject PowerPoint.Application
# Get all objects of type .ppt? in $curr_path and its subfolders

# Get-ChildItem -Path . -Recurse -Filter *.ppt? | ForEach-Object { Write-Output $_.FullName }

Get-ChildItem -Path $curr_path -Recurse -Filter *.ppt? | ForEach-Object {
    Write-Output "Processing" $_.FullName "..."
    # Create a name for the PDF document; they are stored in the invocation folder!
    # If you want them to be created locally in the folders containing the source PowerPoint file, replace $curr_path with $_.DirectoryName
    $src_file = Get-Item -Path $_.FullName
    $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
    if((Test-Path -Path $pdf_filename) -and ($src_file.LastWriteTime -le (Get-Item -Path $pdf_filename).LastWriteTime))  {
        Write-Output "$($pdf_filename) has been already generated. Skip it."
    } else {
        # Open it in PowerPoint
        $document = $ppt_app.Presentations.Open($_.FullName, 3)
        # Save as PDF -- 17 is the literal value of `wdFormatPDF`
        $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
        $document.SaveAs($pdf_filename, $opt)
        # Close PowerPoint file
        $document.Close()
    }
}
# Exit and release the PowerPoint object
$ppt_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt_app)